package com.codefox.controller;

import cn.dsna.util.images.ValidateCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * 用于返回一张验证码图片
 *
 * @author litao
 * @date 2018年9月28日21:22:31
 */
@Controller
@RequestMapping("/front")
public class ImageController extends HttpServlet {

    /**
     * @param req  会话request对象
     * @param resp 会话response对象
     * @throws IOException 可能出现输出失败，此异常一般不处理
     */
    @RequestMapping("/vcode.jpg")
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ValidateCode validateCode = new ValidateCode(160, 60, 4, 3);
        System.out.println(validateCode.getCode());
        HttpSession session = req.getSession();
        session.setAttribute("vcode", validateCode.getCode());
        validateCode.write(resp.getOutputStream());
    }
}
